package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Goods;
import com.example.demo.entity.Orders;
import com.example.demo.entity.ShippingAddress;
import com.example.demo.exception.ApplicationException;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.WalletMapper;
import com.example.demo.service.*;
import com.example.demo.vo.Result;
import com.example.demo.vo.ResultCode;
import jakarta.annotation.Resource;
import org.glassfish.jaxb.core.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.hutool.core.lang.Console.log;

/**
 * @author DLX
 * @data 2024/5/31 23:20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders>  implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private GoodsService goodsService;

    @Resource
    private ShippingAddressService service;

    /**
     *  生成订单
     * @param gid
     * @param uid
     * @param addressId
     * @param num
     * @param address
     * @return
     */
    @Override
    public boolean add(Long gid, Long uid, Long addressId, Long num,String address) {
        //获取商品名称、发货地址、收获地址、支付金额、购买数量
        Goods goods = goodsService.getGoodsById(gid);
        Orders orders = new Orders();
        orders.setGid(gid);
        orders.setUid(uid);
        orders.setMoney(goods.getDiscountedPrice());
        orders.setPayMoney(goods.getDiscountedPrice()  * num);
        orders.setGoodsName(goods.getGoodsName());
        orders.setAddress(address);
        ShippingAddress s = service.selectAddressById(addressId);
        orders.setPicturePath(goods.getPicture());
        orders.setShippingAddress(s.getAddress());
        orders.setCount(num);
        orders.setSid(goods.getBusinessId());
        orders.setAddressId(addressId);
        orders.setSid(goods.getBusinessId());

        LocalDateTime dateTime = LocalDateTime.now();
        orders.setCreateTime(dateTime);
        orders.setUpdateTime(dateTime);

        String orderNumber = String.valueOf(System.currentTimeMillis());
        orders.setOrderNumber(orderNumber);


        return baseMapper.insert(orders) > 0;
    }

    @Resource
    WalletRecordService walletRecordService;

    //变更订单状态
    @Override
    public void update(String status,Long oid) {
        LocalDateTime now = LocalDateTime.now();
        if("已付款".equals(status)){
            //更新钱包记录->同时扣减钱包
            walletRecordService.insertWalletRecord(oid);
        }
        orderMapper.update(status,oid,now);
    }

    //用户订单
    @Override
    public List<Orders> getOrderById(Long uid) {
        // 1. 校验参数
        if (uid < 0) {
            log.warn("参数校验失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 调用mapper
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("uid", uid);
        List<Orders> ordersList = orderMapper.selectByMap(columnMap);

        // 3. 返回结果
        return ordersList;

    }

    //id查询订单
    @Override
    public Orders getOrderByOid(Long oid) {
        if (oid < 0) {
            log.warn("参数校验失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }
        Orders orders = orderMapper.selectById(oid);
        return orders;
    }

    //商家订单
    @Override
    public List<Orders> getOrderBySid(Long sid) {
        // 1. 校验参数
        if ( sid < 0) {
            log.warn("参数校验失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 调用mapper
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("sid", sid);
        List<Orders> ordersList = orderMapper.selectByMap(columnMap);

        // 3. 返回结果
        return ordersList;
    }

    @Override
    public void updateCommit(Integer plusComment,Long orderId) {
        orderMapper.updateCommit(plusComment,orderId);
    }
}

